﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller;
using Entity;

namespace Web
{
    public partial class CustomerInvoiceList : System.Web.UI.Page
    {
        CustInvoiceController customerInvoiceController =
            new CustInvoiceController();
        Helper helper = new Helper(); 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["FlashMessage"] != null)
                {
                    Session["FlashMessage"] = Session["FlashMessage"].ToString();
                    Session["FlashMessageType"] = Session["FlashMessageType"].ToString();
                }
                //Bind all vendor invoices to grid view by default

                IList<CustomerInvoiceEntity> customerInvoiceEntities =
                    customerInvoiceController.FindAllCustomerInvoices();
                if (customerInvoiceEntities.Count > 0)
                {
                    BindGridViewData(customerInvoiceEntities);
                }
                else
                {
                    Session["FlashMessage"] = "Notice: No Customer Invoice Found!";
                    Session["FlashMessageType"] = "notice";
                }


                //Bind all Customer to drop down
                CustomerController CustomerController = new CustomerController();
                IList<CustomerEntity> customerEntities = CustomerController.finAllCustomers();
                this.CustomerDropDownList.DataSource = customerEntities;
                this.CustomerDropDownList.DataValueField = "Id";
                this.CustomerDropDownList.DataTextField = "ShortName";
                this.CustomerDropDownList.DataBind();

            }
            this.PreRender += new EventHandler(UpdateFlashMessage);
        }
        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }
        private void BindGridViewData(IList<CustomerInvoiceEntity> customerInvoiceEntities)
        {
            if(customerInvoiceEntities==null || customerInvoiceEntities.Count<=0)
            {
                Session["FlashMessage"] = "Notice: No Match Record Found!";
                Session["FlashMessageType"] = "notice";
            }
            else
            {
                this.CustomerInvoiceListGridView.DataSource = customerInvoiceEntities;
                this.CustomerInvoiceListGridView.DataBind();    
            }
            
        }

        protected void DateSearchButton_Click(object sender, EventArgs e)
        {
            DateTime fromDate = Convert.ToDateTime(FromTextBox.Text.Trim());
            DateTime toDate = Convert.ToDateTime(ToTextBox.Text.Trim());
            IList<CustomerInvoiceEntity> customerInvoiceEntities =
                customerInvoiceController.FindCustomerInvoiceByDate(fromDate, toDate);
            BindGridViewData(customerInvoiceEntities);
        }

        protected void InvoiceNoSearchButton_Click(object sender, EventArgs e)
        {
            string invoiceNo = InvoiceNoTextBox.Text.Trim().ToUpper();
            IList<CustomerInvoiceEntity> customerInvoiceEntities =
                customerInvoiceController.FindCustomerInvoiceByInvoiceNo(invoiceNo);
            BindGridViewData(customerInvoiceEntities);
        }

        protected void CustomerSearchButton_Click(object sender, EventArgs e)
        {
            int customerId = Convert.ToInt32(CustomerDropDownList.SelectedValue);
            IList<CustomerInvoiceEntity> customerInvoiceEntities =
                customerInvoiceController.FindCustomerInvoiceByCustomer(customerId);
            BindGridViewData(customerInvoiceEntities);
        }

        protected void CustomerInvoiceListGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            CustomerInvoiceListGridView.PageIndex = e.NewPageIndex;
            IList<CustomerInvoiceEntity> customerInvoiceEntities =
                    customerInvoiceController.FindAllCustomerInvoices();
            BindGridViewData(customerInvoiceEntities);
        }
    }
}